//
//  Emotion.swift
//  emojiKeyboardDemo
//
//  Created by Jin on 2018/5/17.
//  Copyright © 2018年 Jin. All rights reserved.
//

import UIKit

class EmotionPackage:NSObject {
    
    var emotions:[Emotion] = [Emotion]()
    
    init(_ name:String) {
        
        super.init()
        
        //1. 最近使用表情
        if name == "" {
            addEmptyEmotion(true)
            return
        }
        //2. 根据name拼接info.plist路径
        let plistPath = Bundle.main.path(forResource: "\(name)/info.plist", ofType: nil, inDirectory: "Emotions.bundle")!
        let array = NSDictionary(contentsOfFile: plistPath)!["emoticons"] as! [[String:String]]
        
        var index = 0
        for var dict in array {
            if let png = dict["png"] {
                dict["png"] = "\(name)/\(png)"
            }
            emotions.append(Emotion(dict: dict))
            
            index += 1
            if index == 20 {
                emotions.append(Emotion(isRemove: true))
                index = 0
            }
        }
        
        //添加空白表情
        addEmptyEmotion(false)
    }
    
    private func addEmptyEmotion(_ isRencently:Bool){
        let count = emotions.count % 21
        if count == 0 && !isRencently {
            return
        }
        for _ in count..<20 {
            emotions.append(Emotion(isEmpty: true))
        }
        emotions.append(Emotion(isRemove: true))
        
    }
    
}
